<?php
require 'vuelta.config.php';

$succ = true;

function user_exists($email, $con) {
  $query = "SELECT email FROM account WHERE email='$email';";
  if(mysql_num_rows($con->query($query))) {
    return true;
  } else {
    return false;
  }
}

function register($data, $con){
  $name = DatabaseConnection::sanitize($data['first_name'] . " ". $data['last_name']);
  $email = DataBaseConnection::sanitize($data['email']);
  $password = md5(DataBaseConnection::sanitize($data['password']));
  $team = DataBaseConnection::sanitize($data['team']);
  $address = DataBaseConnection::sanitize($data['address']);
  $postal_code = DataBaseConnection::sanitize($data['postal_code']);
  $city = DataBaseConnection::sanitize($data['city']);
  $house_phone = DataBaseConnection::sanitize($data['house_phone']);
  $mobile_phone = DataBaseConnection::sanitize($data['mobile_phone']);
  $referral = DataBaseConnection::sanitize($data['referral']);
  $query = "INSERT INTO `account` (
    `id` ,
    `name` ,
    `email` ,
    `password` ,
    `team` ,
    `address` ,
    `postal_code` ,
    `city` ,
    `house_phone` ,
    `mobile_phone` ,
    `referral`
  )
  VALUES (
    NULL, '$name', '$email', '$password', '$team', '$address', '$postal_code', '$city', '$house_phone', '$mobile_phone', '$referral'
  );"; 
  return $con->query($query);
}

$db = Vuelta::get_database_connection();

if(isset($_POST['email'])){
  if(user_exists($_POST['email'], $db)){
    $user_inuse = true;
    $succ = false;
  }
  else {
    $subject = "Activatie Vuelta spel";
    $message = "Open de volgende link in uw browser om uw aanmelding voor het Vuelta tourspel af te maken: ";
    $from = "noreply@vueltaspel.nl";
    $headers = "From:" . $from;
    if(register($_POST, $db)){
      $to = $_POST['email'];
      $link = "http://" . Vuelta::url_path('activation.php') . "?act=" . md5($to);
      $message = $message. $link;
      $succ = mail($to,$subject,$message,$headers);
      if(!$succ) echo $message;
    } else {
      $succ = false;
    }
  }
}

$db->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Vuelta Tourspel - Registreren</title>

  <?php Vuelta::css('css/generic.css'); ?>
  <?php Site::css('header'); ?>
  <?php Vuelta::css('css/register.css'); ?>
</head>

<body>
  <?php Site::load('header'); ?>

  <form class="thin-border content" action="register.php" method="POST">
      <?php if(isset($user_inuse)) { ?>
      <p class="thin-border message">Dit emailadress staat al geregistreerd!</p>
      <?php } else if(!pempty()) {
                if($succ) { ?>
                  <p class="thin-border message">Uw bent geregistreerd. Activeer uw account door de instructies in de email te volgen</p>
      <?php } else { ?>
                  <p class="thin-border message">Er ging iets fout tijdens de registratie. Heeft u alle velden juist ingevuld?</p>
      <?php } } ?>
      <p><label>Email: <input type="email" name="email" required="required" <?php if(!$succ){ echo "value=\"" . $_POST['email']. "\""; } ?> ></label></p>
      <p><label>Wachtwoord: <input type="password" name="password" required="required"></label></p>
      <p><label>Voornaam: <input name="first_name" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['first_name']. "\""; } ?>></label></p>
      <p><label>Achternaam: <input name="last_name" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['last_name']. "\""; } ?>></label></p>
      <p><label>Adres: <input name="address" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['address']. "\""; } ?>></label></p>
      <p><label>Postcode: <input name="postal_code" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['postal_code']. "\""; } ?>></label></p>
      <p><label>Stad: <input name="city" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['city']. "\""; } ?>></label></p>
      <p><label>Huis telefoon: <input type=tel name="house_phone" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['house_phone']. "\""; } ?>></label></p>
      <p><label>Mobiele telefoon: <input type=tel name="mobile_phone" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['mobile_phone']. "\""; } ?>></label></p>
      <p><label>Team: <input name="team" required="required" <?php if(!$succ){ echo "value=\"" .$_POST['team']. "\""; } ?>></label></p>
      <p><label>Hoe bent u bij deze site gekomen? <textarea name="referral" <?php if(!$succ){ echo "value=\"" .$_POST['referral']. "\""; } ?>></textarea></label></p>
      <p><button>Verstuur</button></p>
  </form>
</body>
</html>
